Fix O(n²) complexity for String.prototype.slice() on rope strings#154
Open
robobun wants to merge 1 commit intooven-sh:mainfrom
Open
Fix O(n²) complexity for String.prototype.slice() on rope strings#154robobun wants to merge 1 commit intooven-sh:mainfrom
robobun wants to merge 1 commit intooven-sh:mainfrom
Conversation
When slicing a rope string across fiber boundaries, the previous implementation would resolve the entire rope string, causing O(n) complexity per slice operation. This leads to O(n²) overall complexity when iterating through a concatenated string with slice operations. The fix handles cross-fiber slices by extracting substrings from each relevant fiber and concatenating them into a new rope, avoiding the need to resolve the entire original rope. Before this fix: - Bun: ~2800ms for 50k iterations - Node.js: ~2ms for 50k iterations After this fix, Bun should match Node.js performance. Fixes oven-sh/bun#26682
robobun
pushed a commit
to oven-sh/bun
that referenced
this pull request
Feb 2, 2026
String.prototype.slice() currently has O(n) complexity on rope strings, causing O(n²) overall complexity when iterating through a concatenated string with slice operations. The tests are marked as todo until the WebKit fix is merged: oven-sh/WebKit#154 Once the WebKit PR is merged and the commit hash is updated in cmake/tools/SetupWebKit.cmake, these tests should be enabled. Fixes #26682 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
WalkthroughReworked Changes
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When slicing a rope string across fiber boundaries, the previous implementation would resolve the entire rope string, causing O(n) complexity per slice operation. This leads to O(n²) overall complexity when iterating through a concatenated string with slice operations.
Changes
The fix modifies
tryJSSubstringImplinJSString.hto handle cross-fiber slices by extracting substrings from each relevant fiber and concatenating them into a new rope, avoiding the need to resolve the entire original rope.Performance Impact
Before this fix (50k iterations of slice on a 50k character rope string):
After this fix, Bun should match Node.js performance.
Reproduction
Fixes oven-sh/bun#26682